package com.swjd.jingle.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.swjd.jingle.R;
import com.swjd.jingle.pojo.Goods;
import com.xuexiang.xui.widget.imageview.RadiusImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LvAdapter extends BaseAdapter {
    private FragmentActivity context;
    private List<Goods> goods;

    public LvAdapter(FragmentActivity context, List<Goods> goods) {
        this.context = context;
        this.goods = goods;
    }

    @Override
    public int getCount() {
        return goods.size();
    }

    @Override
    public Object getItem(int position) {
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_lv, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(goods.get(position).getGoodsName());
        holder.tvDesc.setText(goods.get(position).getgDesc());
        holder.tvPrice.setText("￥" + goods.get(position).getPrice());
        holder.tvAddress.setText(goods.get(position).getUser().getAddress());
        holder.tvUser.setText(goods.get(position).getUser().getName());
        switch (position) {
            case 0:
                holder.imgGoods.setImageResource(R.mipmap.aaa033e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 1:
                holder.imgGoods.setImageResource(R.mipmap.ic_meizu16);
                break;
            case 2:
                holder.imgGoods.setImageResource(R.mipmap.ad034e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 3:
                holder.imgGoods.setImageResource(R.mipmap.hxl031e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 4:
                holder.imgGoods.setImageResource(R.mipmap.afg035e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 5:
                holder.imgGoods.setImageResource(R.mipmap.fe039e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 6:
                holder.imgGoods.setImageResource(R.mipmap.ad034e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 7:
                holder.imgGoods.setImageResource(R.mipmap.we038e1a9c801f44b1b8025c53b5632c476822);
                break;
            case 8:
                holder.imgGoods.setImageResource(R.mipmap.se037e1a9c801f44b1b8025c53b5632c476822);
                break;
        }
        return convertView;
    }

    static
    class ViewHolder {
        @BindView(R.id.img_goods)
        RadiusImageView imgGoods;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_desc)
        TextView tvDesc;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.tv_user)
        TextView tvUser;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
